import Keys from "../config/keys";
import RunData from "../config/runData";

/**
 * 拼接url
 */
export default class BaseUrl{
    sessionId = RunData.MyUserInfo?RunData.MyUserInfo.sessionId:undefined;
    areaCode = RunData.areaCode;
    getUrl(){
        var and = "";
        var url = this._getUrl();
        if(!url){
            wx.showToast({title: '未找到配置接口', icon: 'none', duration: 2000});
            return;
        }
        for(let key in this){
            and+="&"+key+"="+this[key];
        }
        and = and.replace(/(^&)/g,'');
        if(and.length<=0){
            return url;
        }
        if (url.match(/(#\/)/g)){
            if (!url.match(/(\?)/g)){
                url+="?"+and;
            }else{
                url+="&"+and;
            }
        }else if (url.match(/(#)/g)){
            if (url.match(/(\?)/g)){
                url = url.replace("#","&"+and+"#");
            }else{
                url = url.replace("#","?"+and+"#");
            }
        }else {
            if (url.match(/(\?)/g)){
                url+="&"+and;
            }else{
                url=url+"?"+and;
            }
        }
        return encodeURI(url);
    }
    _getUrl(){
        try {
            var s = wx.getStorageSync(Keys.downloadMainifest);
            if (s[this.getParamMethod()]) {
                return s[this.getParamMethod()];
            } else {
                return s.coreservice[this.getParamMethod()];
            }
        } catch (e) {
            return null;
        }
    }
    getParamMethod(){
        return null;
    }
}